package org.garbagecan.quartzstudy;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;

public class SchedulerTest {
	
	private static Log log = LogFactory.getLog(SchedulerTest.class);
	
	public static void main(String[] args) throws Exception {
		SchedulerTest test = new SchedulerTest();
//		test.testGetJobDetails();
//		test.testDeleteJob();
		test.testRescheduleJob();
	}
	
	private void testGetJobDetails() throws Exception {
		Scheduler scheduler = TestUtil.getScheduler();
		
		JobDetail jobDetail1 = JobBuilder.newJob(MyJob.class).withIdentity("job1","group1").build();
		JobDetail jobDetail2 = JobBuilder.newJob(MyJob.class).withIdentity("job2","group1").build();

		Trigger trigger1 = TriggerBuilder.newTrigger()
				.withIdentity("trigger1", "group1")
				.startNow()
				.withPriority(5)
				.withSchedule(SimpleScheduleBuilder
					.simpleSchedule()
					.withIntervalInSeconds(2)
					.repeatForever())
				.build();
		Trigger trigger2 = TriggerBuilder.newTrigger()
				.withIdentity("trigger2", "group1")
				.startNow()
				.withPriority(5)
				.withSchedule(SimpleScheduleBuilder
					.simpleSchedule()
					.withIntervalInSeconds(2)
					.repeatForever())
				.build();
		
		scheduler.scheduleJob(jobDetail1, trigger1);
		scheduler.scheduleJob(jobDetail2, trigger2);
		scheduler.start();
		
		log.info("JobDetai 1: " + scheduler.getJobDetail(JobKey.jobKey("job1", "group1")));
		log.info("JobDetai 2: " + scheduler.getJobDetail(JobKey.jobKey("job2", "group1")));
		log.info("JobDetai 3: " + scheduler.getJobDetail(JobKey.jobKey("job3", "group1")));
	}
	
	private void testDeleteJob() throws Exception {
		Scheduler scheduler = TestUtil.getScheduler();
		
		JobDetail jobDetail1 = JobBuilder.newJob(MyJob.class).withIdentity("job1","group1").build();
		JobDetail jobDetail2 = JobBuilder.newJob(MyJob.class).withIdentity("job2","group1").build();

		Trigger trigger1 = TriggerBuilder.newTrigger()
				.withIdentity("trigger1", "group1")
				.startNow()
				.withPriority(5)
				.withSchedule(SimpleScheduleBuilder
					.simpleSchedule()
					.withIntervalInSeconds(2)
					.repeatForever())
				.build();
		Trigger trigger2 = TriggerBuilder.newTrigger()
				.withIdentity("trigger2", "group1")
				.startNow()
				.withPriority(5)
				.withSchedule(SimpleScheduleBuilder
					.simpleSchedule()
					.withIntervalInSeconds(2)
					.repeatForever())
				.build();
		
		scheduler.scheduleJob(jobDetail1, trigger1);
		scheduler.scheduleJob(jobDetail2, trigger2);
		scheduler.start();
		
		Thread.sleep(8000);
		
		log.info("Before delete job: " + TestUtil.getDateTime());
		scheduler.deleteJob(JobKey.jobKey("job1", "group1"));
		scheduler.deleteJob(JobKey.jobKey("job2", "group1"));
		log.info("After delete job : " + TestUtil.getDateTime());

		// delete not exist job, it should be ok.
		scheduler.deleteJob(JobKey.jobKey("jobx", "group1"));
		
		Thread.sleep(1000*60*60);
		
		scheduler.shutdown();
	}
	
	private void testRescheduleJob() throws Exception {
		List<? extends Trigger> triggers;

		Scheduler scheduler = TestUtil.getScheduler();
		
		JobDetail jobDetail1 = JobBuilder.newJob(MyJob.class).withIdentity("job1","group1").build();
		
		Trigger trigger1 = TriggerBuilder.newTrigger()
				.withIdentity("trigger1", "group1")
				.startNow()
				.withPriority(5)
				.withSchedule(SimpleScheduleBuilder
					.simpleSchedule()
					.withIntervalInSeconds(2)
					.repeatForever())
				.build();
		Trigger trigger2 = TriggerBuilder.newTrigger()
				.withIdentity("trigger2", "group1")
				.startNow()
				.withPriority(5)
				.withSchedule(SimpleScheduleBuilder
					.simpleSchedule()
					.withIntervalInSeconds(1)
					.repeatForever())
				.build();
		
		scheduler.scheduleJob(jobDetail1, trigger1);
		scheduler.start();

		triggers = scheduler.getTriggersOfJob(JobKey.jobKey("job1", "group1"));
		for (Trigger trigger : triggers) {
			log.info("Trigger key: " + trigger.getKey());
			log.info("Job key: " + trigger.getJobKey());
		}
		Thread.sleep(8000);
		
		trigger2.getTriggerBuilder().forJob(jobDetail1);
		scheduler.rescheduleJob(TriggerKey.triggerKey("trigger1", "group1"), trigger2);
		log.info("Reschedule job, the interval will be 1s.");

		Thread.sleep(1000*60*60);
		
		scheduler.shutdown();
	}
}
